package com.example.mall.web.controller;

import com.example.mall.domain.RolePermission;
import com.example.mall.dto.RolePermissionDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.RolePermissionQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IRolePermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {

    @Autowired
    public IRolePermissionService rolePermissionService;

    /**
     * 分页查询角色权限表信息
     * @param rolePermissionQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询角色权限表信息",notes = "分页查询角色权限表信息",httpMethod = "GET")
    public JSONResult queryPage(RolePermissionQuery rolePermissionQuery){
        PageDTO<RolePermissionDto> rolePermissionDtoPageDTO = rolePermissionService.queryPage(rolePermissionQuery);
        return JSONResult.success("查询成功",rolePermissionDtoPageDTO);
    }

    /**
     * 根据id查询角色权限表信息
     * @param rolePermissionId
     * @return
     */
    @GetMapping("query/{rolePermissionId}")
    @ApiOperation(value = "根据id查询角色权限表信息",notes = "根据id查询角色权限表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("rolePermissionId")Long rolePermissionId){
        //1.根据id查询用户信息
        if (rolePermissionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ROLEPREMISSIONID_NOT_ID);
        }
        RolePermission rolePermission = rolePermissionService.getById(rolePermissionId);
        return JSONResult.success("查询成功",rolePermission);
    }

    /**
     * 根据id查询角色权限表信息
     * @param rolePermissionId
     * @return
     */
    @GetMapping("queryRolePermissionById/{rolePermissionId}")
    @ApiOperation(value = "根据id查询角色权限表信息",notes = "根据id查询角色权限表信息",httpMethod = "GET")
    public JSONResult queryRolePermissionById(@PathVariable("rolePermissionId")Long rolePermissionId){
        //1.根据id查询用户信息
        if (rolePermissionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ROLEPREMISSIONID_NOT_ID);
        }
        RolePermission rolePermission = rolePermissionService.queryRolePermissionById(rolePermissionId);
        return JSONResult.success("查询成功",rolePermission);
    }

    /**
     * 查询全部角色权限表信息
     * @param
     * @return
     */
    @GetMapping("queryRolePermissionList")
    @ApiOperation(value = "查询全部角色权限表信息",notes = "查询全部角色权限表信息",httpMethod = "GET")
    public JSONResult queryRolePermissionList(){
        List<RolePermission> rolePermissions = rolePermissionService.queryRolePermissionList();
        return JSONResult.success("查询成功",rolePermissions);
    }

    /**
     * 新增角色权限表
     * @param rolePermissionDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增角色权限表",notes = "新增角色权限表",httpMethod = "POST")
    public JSONResult insert(@RequestBody RolePermissionDto rolePermissionDto){
        rolePermissionService.insert(rolePermissionDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改角色权限表信息
     * @param rolePermissionDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改角色权限表信息",notes = "修改角色权限表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody RolePermissionDto rolePermissionDto){
        rolePermissionService.update(rolePermissionDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除角色权限表信息
     * @param rolePermissionId
     * @return
     */
    @PostMapping("delete/{rolePermissionId}")
    @ApiOperation(value = "删除角色权限表信息",notes = "删除角色权限表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("rolePermissionId") Long rolePermissionId) {
        rolePermissionService.delete(rolePermissionId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除角色权限表信息
     * @param rolePermissionIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除角色权限表信息",notes = "批量删除角色权限表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> rolePermissionIds) {
        rolePermissionService.deleteBatch(rolePermissionIds);
        return JSONResult.success("批量删除成功");
    }
}
